//
//  EnableBusLineViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/2.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "EnableBusLineViewController.h"
#import "RegularBusLineViewModel.h"
#import "RegularBusLineModel.h"
#import "RoutePlanningViewController.h"
#import "BusLineViewController.h"
#import "EnableBusLineCell.h"
#import "BusLineViewController.h"
#import "PoisModel.h"
#import "MJRefresh.h"
#import "LoadingTableCell.h"

@interface EnableBusLineViewController ()
@property (strong, nonatomic) IBOutlet UITableView *mainTable;

@property (strong, nonatomic) RegularBusLineViewModel *regularBusLineViewModel;

@end

@implementation EnableBusLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mainTable.backgroundColor = GLOBLE_BACKGROUND_COLOR;
    [self setExtraCellLineHidden:self.mainTable];
    self.mainTable.alpha = 0;
    self.regularBusLineViewModel = [[RegularBusLineViewModel alloc ] init];
    [self loadData];
    
    // 头部刷新
    WEAKSELF
    [self addHeader:^() {
         weakSelf.regularBusLineViewModel.pageIndex = 1;
        [weakSelf loadData];
    } tableView:self.mainTable];
    
    self.mainTable.mj_footer = [MJDIYBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}


-(void)loadMoreData{
    self.regularBusLineViewModel.pageIndex ++;
    [self loadData];
}

-(void)loadData{
    
    self.regularBusLineViewModel.requestWaitLineModel.StartLat=@(self.busLineViewController.startPoisModel.latitude).stringValue;
    self.regularBusLineViewModel.requestWaitLineModel.StartLng=@(self.busLineViewController.startPoisModel.longitude).stringValue;
    self.regularBusLineViewModel.requestWaitLineModel.EndLat  =@(self.busLineViewController.endPoisModel.latitude).stringValue;
    self.regularBusLineViewModel.requestWaitLineModel.EndLng  =@(self.busLineViewController.endPoisModel.longitude).stringValue;
    WEAKSELF
    [self.regularBusLineViewModel getWaitLineListResultBlock:^(NSInteger resultType, id data, NSString *errorMsg) {
        
        [weakSelf.mainTable.mj_header endRefreshing];
        [weakSelf.mainTable.mj_footer endRefreshing];
        if (resultType == 1 || resultType == 3) {
            
            weakSelf.mainTable.alpha = 1;
            [weakSelf.mainTable reloadData];
            
            if (resultType == 3) {
                weakSelf.mainTable.mj_footer.hidden = YES;
            }else{
                weakSelf.mainTable.mj_footer.hidden = NO;

            }
            
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
    }];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    RegularBusLineModel *model = self.regularBusLineViewModel.waitLineList[indexPath.row];

    RoutePlanningViewController *routePlanningViewController = (RoutePlanningViewController *)[RoutePlanningViewController getViewControllerFromSbName:@"RoutePlanning" viewControllerName:@"RoutePlanningViewController"];
    routePlanningViewController.routePlanningStatus = RoutePlanningWaite;
    routePlanningViewController.lineId = @(model.LineId).stringValue;
    routePlanningViewController.BCID = model.BCID;
    routePlanningViewController.StartStationId = model.StartStationId;
    routePlanningViewController.EndStationId  = model.EndStationId;
    
    [self.busLineViewController.navigationController pushViewController:routePlanningViewController animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.regularBusLineViewModel.waitLineList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EnableBusLineCell *lineCell = [tableView dequeueReusableCellWithIdentifier:[EnableBusLineCell cellIdentifier]];
    
    [lineCell showModel:self.regularBusLineViewModel.waitLineList[indexPath.row]];
    return lineCell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
